package cn.escco.ocr.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import cn.escco.ocr.dto.bo.*;
import cn.escco.ocr.dto.po.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import cn.escco.ocr.dto.pa.JybBizHdcpOddsParam;
import cn.escco.ocr.dto.pa.JybLoadDataParam;
import cn.escco.ocr.dto.pa.JybMstPlatParam;
import cn.escco.ocr.dto.pa.JybMstPlatPlayParam;
import cn.escco.ocr.dto.pa.JybMstPlatTypeLeaParam;
import cn.escco.ocr.dto.pa.JybMstPlatTypeLeaTeamParam;
import cn.escco.ocr.dto.pa.JybMstPlatTypeLeaTeamVsParam;
import cn.escco.ocr.dto.pa.JybMstPlatTypeParam;
import cn.escco.ocr.dto.vo.PlatPlayHdcpLineVo;
import cn.escco.ocr.dto.vo.PlatPlayHdcpListVo;
import cn.escco.ocr.dto.vo.PlatPlayHdcpOddsVo;
import cn.escco.ocr.dto.vo.PlatPlayHdcpVo;
import cn.escco.ocr.dto.vo.PlatPlayVo;
import cn.escco.ocr.dto.vo.PlatTypeLeaTeamVsVo;
import cn.escco.ocr.dto.vo.PlatTypeLeaVo;
import cn.escco.ocr.dto.vo.PlatTypeVo;
import cn.escco.ocr.dto.vo.PlatVo;
import cn.escco.ocr.service.JybCoreService;

@Service
public class JybCoreServiceImpl implements JybCoreService {

    @Override
    public PlatPlayHdcpListVo acquirePlatPlayHdcps(String baseDate, String platCode, String typeCode, String leaCode,
                                                   String teamCodeLeft, String teamCodeRight, String playCode,
                                                   String acquireFlag, String timeStampPre) {
        PlatPlayHdcpListVo vo = new PlatPlayHdcpListVo();
        boolean initLineFlag = true; // 图形线条加载标识
        String timeStampStart = baseDate.concat("000000");
        String timeStampEnd = baseDate.concat("235959");
        List<PlatPlayHdcpVo> hdcpList = jybBizHdcpOddsDao.getPlatPlayHdcps(baseDate, platCode, typeCode, leaCode,
                teamCodeLeft, teamCodeRight, playCode, timeStampEnd);

        // 非初期加载
        if (!"Y".equals(acquireFlag)) {
            List<PlatPlayHdcpVo> hdcpListPre = jybBizHdcpOddsDao.getPlatPlayHdcps(baseDate, platCode, typeCode, leaCode,
                    teamCodeLeft, teamCodeRight, playCode, timeStampPre);
            initLineFlag = hdcpListPre.size() != hdcpList.size();
            timeStampStart = timeStampPre;
        }

        Map<String, String> mapHdcp = new HashMap<>();
        for (PlatPlayHdcpVo hdcp : hdcpList)
            mapHdcp.put(hdcp.getPlayHdcp(), hdcp.getPlayHdcp());

        List<PlatPlayHdcpLineVo> lineVoList = new ArrayList<>();
        if (initLineFlag)
            hdcpList.forEach(hdcp -> {
                lineVoList.add(new PlatPlayHdcpLineVo());
                lineVoList.add(new PlatPlayHdcpLineVo());
            });
        vo.setLineVoList(lineVoList);

        List<List<String>> hdcpVoList = new ArrayList<>();
        if (initLineFlag) {
            List<String> arr = new ArrayList<>();
            arr.add("product");
            for (PlatPlayHdcpVo hdcp : hdcpList) {
                arr.add(("←").concat(hdcp.getPlayHdcp()));
                arr.add(hdcp.getPlayHdcp().concat("→"));
            }
            hdcpVoList.add(arr);
        }
        List<PlatPlayHdcpOddsVo> hdcpOddsList = jybBizHdcpOddsDao.getPlatPlayHdcpOddss(baseDate, platCode, typeCode,
                leaCode, teamCodeLeft, teamCodeRight, playCode, timeStampStart, timeStampEnd);
        String timeStampPreNew = timeStampPre;
        for (PlatPlayHdcpOddsVo odds : hdcpOddsList) {
            List<String> arr = new ArrayList<>();
            arr.add(odds.getTimeStamp());
            timeStampPreNew = odds.getTimeStamp();
            for (PlatPlayHdcpVo hdcp : hdcpList) {
                if (odds.getPlayHdcp().equals(hdcp.getPlayHdcp())) {
                    arr.add(odds.getOddsLeft().toPlainString());
                    arr.add(odds.getOddsRight().toPlainString());
                } else {
                    arr.add(BigDecimal.ZERO.toPlainString());
                    arr.add(BigDecimal.ZERO.toPlainString());
                }
            }
            hdcpVoList.add(arr);
        }
        vo.setHdcpVoList(hdcpVoList);
        vo.setLeaName(jybCoreServiceImpl.getLeaName(platCode, typeCode, leaCode));
        vo.setTeamNameLeft(jybCoreServiceImpl.getTeamName(platCode, typeCode, leaCode, teamCodeLeft));
        vo.setTeamNameRight(jybCoreServiceImpl.getTeamName(platCode, typeCode, leaCode, teamCodeRight));
        JybBizAmidithion amidithion = jybCoreServiceImpl.getAmidithion(baseDate, platCode, typeCode, leaCode, teamCodeLeft, teamCodeRight);
        vo.setScoreLeft(StringUtils.EMPTY);
        vo.setScoreRight(StringUtils.EMPTY);
        if (amidithion != null) {
            vo.setScoreLeft(amidithion.getScoreLeft().toString());
            vo.setScoreRight(amidithion.getScoreRight().toString());
        }
        vo.setPlayName(jybCoreServiceImpl.getPlayName(platCode, playCode));
        vo.setTimeStampPre(timeStampPreNew);

        return vo;
    }

    @Override
    public List<PlatPlayVo> acquirePlatPlays(String baseDate, String platCode, String typeCode, String leaCode,
                                             String teamCodeLeft, String teamCodeRight) {
        return jybBizHdcpOddsDao.getPlatPlays(baseDate, platCode, typeCode, leaCode, teamCodeLeft, teamCodeRight);
    }

    @Override
    public List<PlatTypeLeaTeamVsVo> acquirePlatTypeLeaTeamVss(String baseDate, String platCode, String typeCode,
                                                               String leaCode) {
        return jybBizHdcpOddsDao.getPlatTypeLeaTeamVss(baseDate, platCode, typeCode, leaCode);
    }

    @Override
    public List<PlatTypeLeaVo> acquirePlatTypeLeas(String baseDate, String platCode, String typeCode) {
        return jybBizHdcpOddsDao.getPlatTypeLeas(baseDate, platCode, typeCode);
    }

    @Override
    public List<PlatTypeVo> acquirePlatTypes(String baseDate, String platCode) {
        return jybBizHdcpOddsDao.getPlatTypes(baseDate, platCode);
    }

    @Override
    public List<PlatVo> acquirePlats(String baseDate) {
        return jybBizHdcpOddsDao.getPlats(baseDate);
    }

    @Override
    public List<String> getBaseDates() {
        return jybBizHdcpOddsDao.getBaseDates();
    }

    @Override
    @Transactional
    public void loadDatas(JybLoadDataParam loadData) {

        JybMstPlatParam mp = loadData.getMstPlat();
        // 数据平台：手工维护
        JybMstPlat plat = jybCoreServiceImpl.getByName(mp);
        if (plat == null)
            return;
        List<JybMstPlatTypeParam> mpTypeList = mp.getMstPlatTypeList();
        for (JybMstPlatTypeParam mpType : mpTypeList) {
            // 平台分类：手工维护
            JybMstPlatType type = jybCoreServiceImpl.getByName(mpType, plat);
            if (type == null)
                continue;
            List<JybMstPlatTypeLeaParam> mptLeaList = mpType.getMstPlatTypeLeaList();
            for (JybMstPlatTypeLeaParam mptLea : mptLeaList) {
                // 平台分类联赛：手工维护
                JybMstPlatTypeLea lea = jybCoreServiceImpl.getByName(mptLea, type);
                if (lea == null)
                    continue;
                List<JybMstPlatTypeLeaTeamVsParam> mptlTeamVsList = mptLea.getMstPlatTypeLeaTeamVsList();
                for (JybMstPlatTypeLeaTeamVsParam mptlTeamVs : mptlTeamVsList) {
                    // 平台分类联赛球队：程序维护
                    JybMstPlatTypeLeaTeam mptlTeamLeft = jybCoreServiceImpl.getByNameAndAdd(mptlTeamVs.getTeamLeft(),
                            lea);
                    if (mptlTeamLeft == null)
                        continue;
                    JybMstPlatTypeLeaTeam mptlTeamRight = jybCoreServiceImpl.getByNameAndAdd(mptlTeamVs.getTeamRight(),
                            lea);
                    if (mptlTeamRight == null)
                        continue;
                    List<JybMstPlatPlayParam> mpPlayList = mptlTeamVs.getMstPlatPlayList();
                    for (JybMstPlatPlayParam mpPlay : mpPlayList) {
                        // 平台玩法：手工维护
                        JybMstPlatPlay play = jybCoreServiceImpl.getByName(mpPlay, plat);
                        if (play == null)
                            continue;
                        List<JybBizHdcpOddsParam> bhoList = mpPlay.getBizHdcpOddsList();
                        for (JybBizHdcpOddsParam bho : bhoList) {
                            // 盘口赔率：程序维护
                            jybCoreServiceImpl.add(loadData, mptlTeamLeft, mptlTeamRight, play, bho);
                            break;
                        }
                    }
                }
            }
        }
    }

    /**
     * 添加盘口赔率
     *
     * @param loadData
     * @param mptlTeamLeft
     * @param mptlTeamRight
     * @param play
     * @param bho
     * @return
     */
    public int add(JybLoadDataParam loadData, JybMstPlatTypeLeaTeam mptlTeamLeft, JybMstPlatTypeLeaTeam mptlTeamRight,
                   JybMstPlatPlay play, JybBizHdcpOddsParam bho) {
        JybBizHdcpOdds po = new JybBizHdcpOdds();
        BeanUtils.copyProperties(loadData, po);
        BeanUtils.copyProperties(mptlTeamLeft, po);
        BeanUtils.copyProperties(play, po);
        BeanUtils.copyProperties(bho, po);
        po.setTeamCodeLeft(mptlTeamLeft.getTeamCode());
        po.setTeamCodeRight(mptlTeamRight.getTeamCode());

        JybBizHdcpOdds poCheck = new JybBizHdcpOdds();
        BeanUtils.copyProperties(po, poCheck);
        poCheck.setTimeStamp(jybBizHdcpOddsDao.getMaxTimeStamp(po.getHdcpOddsDate(), po.getPlatCode(), po.getTypeCode(),
                po.getLeaCode(), po.getTeamCodeLeft(), po.getTeamCodeRight(), po.getPlayCode()));
        QueryWrapper<JybBizHdcpOdds> query = new QueryWrapper<>();
        query.setEntity(poCheck);
        JybBizHdcpOdds poGet = jybBizHdcpOddsDao.selectOne(query);
        if (poGet != null && poGet.getPlayHdcp().equals(po.getPlayHdcp())
                && poGet.getOddsLeft().compareTo(po.getOddsLeft()) == 0
                && poGet.getOddsRight().compareTo(po.getOddsRight()) == 0)
            return 0;

        return jybBizHdcpOddsDao.insert(po);
    }

    /**
     * 获取平台玩法
     *
     * @param param
     * @param jybMstPlat
     * @return
     */
    public JybMstPlatPlay getByName(JybMstPlatPlayParam param, JybMstPlat jybMstPlat) {
        JybMstPlatPlay po = new JybMstPlatPlay();
        BeanUtils.copyProperties(param, po);
        BeanUtils.copyProperties(jybMstPlat, po);
        po.setPlayName(this.replaceAllStr(po.getPlayName()));
        QueryWrapper<JybMstPlatPlay> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatPlayDao.selectOne(query);
    }

    /**
     * 获取平台分类联赛球队
     *
     * @param param
     * @param jybMstPlatTypeLea
     * @return
     */
    public synchronized JybMstPlatTypeLeaTeam getByNameAndAdd(JybMstPlatTypeLeaTeamParam param,
                                                 JybMstPlatTypeLea jybMstPlatTypeLea) {
        JybMstPlatTypeLeaTeam po = new JybMstPlatTypeLeaTeam();
        BeanUtils.copyProperties(param, po);
        BeanUtils.copyProperties(jybMstPlatTypeLea, po);
        po.setTeamName(this.replaceAllStr(po.getTeamName()));
        QueryWrapper<JybMstPlatTypeLeaTeam> query = new QueryWrapper<>();
        query.setEntity(po);
        if (jybMstPlatTypeLeaTeamDao.selectOne(query) == null) {
            JybMstPlatTypeLeaTeam poByCount = new JybMstPlatTypeLeaTeam();
            BeanUtils.copyProperties(jybMstPlatTypeLea, poByCount);
            QueryWrapper<JybMstPlatTypeLeaTeam> queryByCount = new QueryWrapper<>();
            queryByCount.setEntity(poByCount);
            int iCount = jybMstPlatTypeLeaTeamDao.selectCount(queryByCount);
            po.setTeamCode(String.valueOf(iCount));
            if (iCount < 10)
                po.setTeamCode("0".concat(String.valueOf(iCount)));
            po.setTeamSort(iCount);
            po.setTeamFlag("9");
            jybMstPlatTypeLeaTeamDao.insert(po);
        }
        return jybMstPlatTypeLeaTeamDao.selectOne(query);
    }

    /**
     * 获取平台分类联赛名称
     * 
     * @param platCode
     * @param playCode
     * @return
     */
    public String getPlayName(String platCode, String playCode) {
        JybMstPlatPlay po = new JybMstPlatPlay();
        po.setPlatCode(platCode);
        po.setPlayCode(playCode);
        QueryWrapper<JybMstPlatPlay> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatPlayDao.selectOne(query).getPlayName();
    }

    /**
     * 获取赛果
     *
     * @param baseDate
     * @param platCode
     * @param typeCode
     * @param leaCode
     * @param teamCodeLeft
     * @param teamCodeRight
     * @return
     */
    public JybBizAmidithion getAmidithion(String baseDate, String platCode, String typeCode, String leaCode, String teamCodeLeft, String teamCodeRight) {
        JybBizAmidithion po = new JybBizAmidithion();
        po.setHdcpOddsDate(baseDate);
        po.setPlatCode(platCode);
        po.setTypeCode(typeCode);
        po.setLeaCode(leaCode);
        po.setTeamCodeLeft(teamCodeLeft);
        po.setTeamCodeRight(teamCodeRight);
        QueryWrapper<JybBizAmidithion> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybBizAmidithionDao.selectOne(query);
    }

    /**
     * 获取平台分类联赛球队名称
     * 
     * @param platCode
     * @param typeCode
     * @param leaCode
     * @param teamCode
     * @return
     */
    public String getTeamName(String platCode, String typeCode, String leaCode, String teamCode) {
        JybMstPlatTypeLeaTeam po = new JybMstPlatTypeLeaTeam();
        po.setPlatCode(platCode);
        po.setTypeCode(typeCode);
        po.setLeaCode(leaCode);
        po.setTeamCode(teamCode);
        QueryWrapper<JybMstPlatTypeLeaTeam> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatTypeLeaTeamDao.selectOne(query).getTeamName();
    }

    /**
     * 获取平台分类联赛名称
     * 
     * @param platCode
     * @param typeCode
     * @param leaCode
     * @return
     */
    public String getLeaName(String platCode, String typeCode, String leaCode) {
        JybMstPlatTypeLea po = new JybMstPlatTypeLea();
        po.setPlatCode(platCode);
        po.setTypeCode(typeCode);
        po.setLeaCode(leaCode);
        QueryWrapper<JybMstPlatTypeLea> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatTypeLeaDao.selectOne(query).getLeaName();
    }

    /**
     * 获取平台分类联赛
     *
     * @param param
     * @param jybMstPlatType
     * @return
     */
    public JybMstPlatTypeLea getByName(JybMstPlatTypeLeaParam param, JybMstPlatType jybMstPlatType) {
        JybMstPlatTypeLea po = new JybMstPlatTypeLea();
        BeanUtils.copyProperties(param, po);
        BeanUtils.copyProperties(jybMstPlatType, po);
        po.setLeaName(this.replaceAllStr(po.getLeaName()));
        QueryWrapper<JybMstPlatTypeLea> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatTypeLeaDao.selectOne(query);
    }

    /**
     * 获取平台分类
     *
     * @param param
     * @param jybMstPlat
     * @return
     */
    public JybMstPlatType getByName(JybMstPlatTypeParam param, JybMstPlat jybMstPlat) {
        JybMstPlatType po = new JybMstPlatType();
        BeanUtils.copyProperties(param, po);
        BeanUtils.copyProperties(jybMstPlat, po);
        po.setTypeName(this.replaceAllStr(po.getTypeName()));
        QueryWrapper<JybMstPlatType> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatTypeDao.selectOne(query);
    }

    /**
     * 获取数据平台
     *
     * @param param
     * @return
     */
    public JybMstPlat getByName(JybMstPlatParam param) {
        JybMstPlat po = new JybMstPlat();
        BeanUtils.copyProperties(param, po);
        QueryWrapper<JybMstPlat> query = new QueryWrapper<>();
        query.setEntity(po);
        return jybMstPlatDao.selectOne(query);
    }

    private String replaceAllStr(String str) {
        String retStr = str;
        retStr = retStr.replaceAll("\\s+", "");
        retStr = retStr.replaceAll("（", "(");
        retStr = retStr.replaceAll("）", ")");
        return retStr;
    }

    @Resource
    JybCoreServiceImpl               jybCoreServiceImpl;
    @Resource
    private JybMstPlatDao            jybMstPlatDao;
    @Resource
    private JybMstPlatTypeDao        jybMstPlatTypeDao;
    @Resource
    private JybMstPlatTypeLeaDao     jybMstPlatTypeLeaDao;
    @Resource
    private JybMstPlatTypeLeaTeamDao jybMstPlatTypeLeaTeamDao;
    @Resource
    private JybMstPlatPlayDao        jybMstPlatPlayDao;
    @Resource
    private JybBizHdcpOddsDao        jybBizHdcpOddsDao;
    @Resource
    private JybBizAmidithionDao      jybBizAmidithionDao;
}
